/**
 * 
 */
package org.hehui.supervisorysystem.login;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.hehui.supervisorysystem.util.CacheImage;
import org.hehui.supervisorysystem.util.IAppConstants;
import org.hehui.supervisorysystem.util.IImageKey;



/**
 * @author hehui
 *
 */
public class LoginDialog extends Dialog {
	
	private Composite container;
	private Text userNameText;
	private Text passwordText;
	
	private ProgressBar progressBar;
	
	boolean isSuccesseful = false;
	


	/**
	 * @param parentShell
	 */
	public LoginDialog(Shell parentShell) {
		super(parentShell);
	}


	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "Login", true);
		createButton(parent, IDialogConstants.CANCEL_ID, "Cancel", false);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		final GridLayout grideLayout = new GridLayout();
		grideLayout.numColumns = 2;
		grideLayout.marginRight = 6;
		container.setLayout(grideLayout);
		
		
		final CLabel userNameLabel = new CLabel(container,SWT.NONE);
		userNameLabel.setText("Name:");	
		userNameLabel.setImage(CacheImage.getINSTANCE().getImage(IAppConstants.APPLICATION_ID,IImageKey.USER_NAME));
		
		userNameText = new Text(container,SWT.BORDER);
		userNameText.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		userNameText.setText("hehui");
		
		final CLabel passwordLabel = new CLabel(container,SWT.NONE);
		passwordLabel.setText("Password:");
		passwordLabel.setImage(CacheImage.getINSTANCE().getImage(IAppConstants.APPLICATION_ID, IImageKey.PASSWORD));
		
		passwordText = new Text(container,SWT.BORDER);
		passwordText.setEchoChar('*');
		passwordText.setLayoutData(new GridData(GridData.FILL,GridData.CENTER,true,false));
		passwordText.setText("hehui");
		
		progressBar = new ProgressBar(container,SWT.INDETERMINATE);
		GridData pbGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		pbGridData.horizontalSpan=2;
		progressBar.setLayoutData(pbGridData);
		progressBar.setVisible(false);
		
		
			return container;
				
	}

//	@Override
//	protected Point getInitialSize() {
//		return new Point(350,160);
//	}

	@Override
	protected void okPressed() {

		final String username = userNameText.getText().trim();
		final String password = passwordText.getText().trim();
		MessageBox mb;
		
		if(username == null || username.trim().length() == 0){
			mb = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.OK);
			mb.setText("Error!");
			mb.setMessage("Invalid User Name!");
			mb.open();
			userNameText.setFocus();
			return;
		}
		
		if(password == null || password.trim().length() == 0){
			mb = new MessageBox(getShell(),SWT.ICON_ERROR|SWT.OK);
			mb.setText("Error!");
			mb.setMessage("Invalid Password!");
			mb.open();
			passwordText.setFocus();
			return;
		}
		
		
		SaveLogInfo.getInstance().setUsername(username);
		SaveLogInfo.getInstance().setPassword(password);
		
		
	/*new Thread(){
			public void run(){
				login.isValidate();
				//progressBar.setVisible(true);
			isSuccesseful = login.haveLoggedin();
			}
		}.start();
		*/
		
		
				
		super.okPressed();		
		
	}
	

	protected void configureShell(Shell newShell){
		super.configureShell(newShell);
		newShell.setText("Login");
		newShell.setImage(CacheImage.getINSTANCE().getImage(IAppConstants.APPLICATION_ID, IImageKey.APP_WINDOW_ICON));
		
	}


	public ProgressBar getProgressBar() {
		return progressBar;
	}



	
	
	
/*	private boolean isValidate(final String username, final String password) {
		ProgressMonitorDialog pmd = new ProgressMonitorDialog(null);
		IRunnableWithProgress rwp = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				isSuccesseful = exeLogin(monitor, username, password);

			}
		};
		try {
			pmd.run(true, false, rwp);
		} catch (Exception e) {
			isSuccesseful = false;
		}
		return isSuccesseful;

	}*/
	
	/*private boolean exeLogin( final String username, final String password) {
		boolean tag = false;
		try {
				
			Session session = HibernateSessionFactory
					.getSession("hibernate_mysql.cfg.xml");

			
			Query query = session.getNamedQuery("org.hehui.memory.Login.exeLogin");								
			query.setString(0, username);
			query.setString(1, password);
			
			Iterator iterate = query.iterate();
			if (iterate.hasNext()) {
				User user = (User) iterate.next();
				SaveLogInfo.getInstance().setUsertag(user.getUserid());
				tag = true;
			}
			
			HibernateSessionFactory.closeSession();
		} catch (Exception e) {
			tag = false;
		} 

		
		return tag;
	}
	*/
	

/*	private boolean exeLogin(final IProgressMonitor monitor, String username,
			String password) {
		boolean tag = false;
		try {
			monitor.beginTask("正在登录数据库，请稍候。。。", IProgressMonitor.UNKNOWN);
			monitor.subTask("创建Hibernate工厂。。。");
				
			Session session = HibernateSessionFactory
					.getSession("hibernate_mysql.cfg.xml");
			monitor.subTask("验证用户名、密码。。。");
		
			//String hql = "from User where username=:user and password=:pwd";
			//Query query = session.createQuery(hql);
			//query.setString("usr", username);
			//query.setString("pwd", password);
			
			Query query = session.getNamedQuery("org.hehui.memory.Login.exeLogin");								
			query.setString(0, username);
			query.setString(1, password);
			
			Iterator iterate = query.iterate();
			if (iterate.hasNext()) {
				User user = (User) iterate.next();
				SaveLogInfo.getInstance().setUsertag(user.getUserid());
				tag = true;
			}
			monitor.subTask("销毁Hibernate Session。。。");
			HibernateSessionFactory.closeSession();
		} catch (Exception e) {
			tag = false;
		} finally {
			monitor.done();
		}
		return tag;
	}
	*/
	

	

}
